# -*- coding: utf-8 -*-
"""
Created on Wed Mar 22 11:26:17 2023

@author: skunk69
"""

from pathlib import Path

def file_list_handle(path, sub_dir=False, suffix_list=[]):
	"""
	path：输入路径，支持文件路径和文件夹路径	
    sub_dir：当为True时含子目录，为False时不含子目录 	
    suffix_list：文件类型列表，按要求的列出全部符合条件的文件，为空时列出全部文件，如：[".xlsx",".xls"]
	"""
	file_list = []
	if not suffix_list:
		if sub_dir:
			[suffix_list.append(Path(f).suffix) for f in Path(path).glob(f"**\*.*") if Path(f).is_file()]
		else:
			[suffix_list.append(Path(f).suffix) for f in Path(path).glob(f"*.*") if Path(f).is_file()]
		suffix_list = list(set(suffix_list))
        
# 	[print(i) for i in suffix_list]
    
	if Path(path).exists():
		# 目标为文件夹
		if Path(path).is_dir():
			if sub_dir:
				for i in suffix_list:
					[file_list.append(str(f)) for f in Path(path).glob(f"**\*{i}")]
			else:
				[file_list.append(str(f)) for f in Path(path).iterdir() if Path(f).is_file and f.suffix in suffix_list]
		elif Path(path).is_file():
			file_list = [path]

		# 去除临时文件
		file_list_temp = []
		for y in file_list:
			if "~$" in Path(y).stem:
				continue
			file_list_temp.append(y)
		file_list = file_list_temp
		return file_list
	else:
		print("输入有误！")
		return []

if __name__ == '__main__':
    path = Path.cwd()
    file_list = file_list_handle(path)
    print('the files are {}'.format(file_list))
    print(f'the length of file_list is {len(file_list)}')

    file_list = file_list_handle(path, sub_dir=True)
    print('the files (and subfolders) are {}'.format(file_list))
    print(f'the length of file_list is {len(file_list)}')